package com.antdesignbackend.backend.common.web;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.config.WebConfig;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Controller
@RequestMapping("/file")
public class FileController {
    /**
     * 文件上传
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    @AuthToken
    public Map<String,String> uploadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //文件大小不能大于 30M
        int fileSize = 20 * 1024 * 1024;

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8") ;
        long random =System.currentTimeMillis();
        String dateKey =random+"";
        String localPathDir="";
        String newFileName="";
        Map<String,String> map = new HashMap<>();
        if(!WebConfig.uploadFile.equals(""))
        {
            localPathDir=WebConfig.uploadFile;
        }
        else
        {
            localPathDir= ResourceUtils.getURL("classpath:").getPath()+"enclosure/";
        }
        File logoSaveFile = new File(localPathDir);
        if(!logoSaveFile.exists()){
            logoSaveFile.mkdirs();
        }
        if (request instanceof MultipartHttpServletRequest) {
            MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
            Iterator iter = mr.getFileMap().values().iterator();
            if (iter.hasNext()) {
                MultipartFile file = (MultipartFile) iter.next();

                if (file.getSize() > fileSize){
                    //文件大小过大
                    map.put("code","500");
                    map.put("msg","文件不能超过10M");
                    return map;
                }
                String oname = file.getOriginalFilename();
                String fileTyle=oname.substring(oname.lastIndexOf("."),oname.length());
                fileTyle=fileTyle.toLowerCase();
                newFileName=dateKey+fileTyle;
                String fileName = localPathDir+newFileName;
                InputStream inputstream =file.getInputStream();
                byte[] bytes = new byte[1024];
                FileOutputStream outstream = new FileOutputStream(fileName);
                int index;
                while ((index = inputstream.read(bytes)) != -1) {
                    outstream.write(bytes, 0, index);
                    outstream.flush();
                }
                outstream.close();
                inputstream.close();
            }
        }
        if(newFileName!=null && !newFileName.equals(""))
        {
            map.put("code","200");
            map.put("fileName",newFileName);
            map.put("httpUrl",WebConfig.host+"enclosure/"+newFileName);
        }
        return map;
    }
}
